home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / Sample Code / Snippets / QuickDraw / CopyDeepMask / CopyDeepMask.c next >
Encoding:
C/C++ Source or Header  |  1994-12-14  |  5.7 KB  |  231 lines  |  [TEXT/MPS ]

  1. /*****************************************************************************************
  2.      Snippet:            CopyDeepMask Demo
  3.      
  4.      Description:        This snippet demonstrates the use of CopyDeepMask using 2 PICTS;
  5.                          one a photograph and the other a triangular mask.     It uses 2 offscreen
  6.                          gworlds to hold the source and mask pixmaps.  CopyDeepMask is then used
  7.                          to create the masked image and display it in the application window.  The
  8.                          source, mask, and destination rectangles are all the same size in order
  9.                          avoid altering pixel sizes.
  10.                          
  11.      Programmer:            Kevin Mellander
  12.      Organization:        Apple Computer, Inc.
  13.      Department:            Developer Technical Support, DTS
  14.      Language/Envir.:    C/Think C Version 6.0.1
  15.      Date Created:        11/22/93
  16.     Date Modified:        12/14/94 Fixed a bug in doEventLoop that was causing a bus error using 
  17.                                  EvenBetterBusError (bad things happen when you attempt to use an 
  18.                                  unitialized EventRecord!)
  19.                         
  20.  
  21.  *****************************************************************************************/
  22.  
  23. #include <Quickdraw.h>
  24. #include <QDOffscreen.h>
  25. #include <Windows.h>
  26. #include <Dialogs.h>
  27. #include <Menus.h>
  28. #include <Types.h>
  29. #include <Memory.h>
  30. #include <Fonts.h>
  31. #include <OSEvents.h>
  32. #include <ToolUtils.h>
  33.  
  34. #define windID 128
  35. #define inFront -1
  36. #define sleepTime 30
  37.  
  38. #define     srcPicID    130
  39. #define     maskPicID    131
  40.  
  41. Boolean                    continueThis;
  42. WindowPtr                mainWindow;
  43.  
  44. void InitToolbox(void);
  45. void doEventLoop(void);
  46. void doDrag (WindowPtr winPtr,Point mouseLoc);
  47. void doUpdateEvent(EventRecord *event);
  48.  
  49.  
  50. void main(void)
  51. {
  52.     
  53.     InitToolbox();
  54.         
  55.     mainWindow = GetNewCWindow(windID,nil,(WindowPtr)inFront);
  56.     
  57.     continueThis = true;
  58.     
  59.     doEventLoop();
  60. }
  61.  
  62. void InitToolbox(void)
  63. {
  64.     MoreMasters();
  65.     MoreMasters();
  66.     MoreMasters();
  67.     
  68.     MaxApplZone();
  69.  
  70.     InitGraf((Ptr)&qd.thePort);
  71.     InitFonts();
  72.     InitWindows();
  73.     InitMenus();
  74.     TEInit();
  75.     InitCursor();
  76.     InitDialogs((long)nil);
  77.     FlushEvents(everyEvent, 0);
  78.     
  79. }
  80.  
  81. void doEventLoop(void)
  82. {
  83.     EventRecord    event;
  84.     WindowPtr    window;
  85.     short        hitArea;
  86.         
  87.     
  88.     while (continueThis)
  89.     {    
  90.         if (WaitNextEvent(everyEvent,&event,sleepTime,nil))
  91.             
  92.             if (event.what == updateEvt)    
  93.                 doUpdateEvent(&event);
  94.                 
  95.             else if (event.what == mouseDown)
  96.             {
  97.                 hitArea = FindWindow(event.where,&window);
  98.                 
  99.                 if (hitArea == inDrag)
  100.                     doDrag(window,event.where);
  101.                     
  102.                 else if (hitArea == inGoAway)
  103.                     if (TrackGoAway (window,event.where))
  104.                         return;
  105.             }
  106.             
  107.     } 
  108. }
  109.         
  110. void doDrag (WindowPtr winPtr,Point mouseLoc)
  111. {
  112.     Rect     dragRect;
  113.     Rect    bndsRect;
  114.     
  115.     bndsRect = qd.screenBits.bounds; /*screenBits is a QuickDraw global variable with the same structure as portBits (BitMap)*/
  116.     InsetRect(&dragRect,10,10);
  117.     DragWindow(winPtr,mouseLoc,&bndsRect);
  118.     
  119. }
  120.  
  121. void doUpdateEvent(EventRecord *event)
  122. {
  123.     Rect            bndsRectSrc,bndsRectMask,srcRect,maskRect,destRect;
  124.     QDErr            gWorldErr;
  125.     GWorldPtr        offscreenGWorldSource;
  126.     GWorldPtr        offscreenGWorldMask;
  127.     short            pixelDepthSource = 32;
  128.     short            pixelDepthMask = 1;
  129.     GWorldFlags        flags = 0;
  130.     GDHandle        currGDevice;
  131.     GWorldPtr        currGWorldPort;
  132.     PicHandle        srcPicHdl;
  133.     PicHandle        maskPicHdl;
  134.     PixMapHandle    srcPMHdl;
  135.     PixMapHandle    maskPMHdl;
  136.     Boolean            pmLock= false;
  137.  
  138.     
  139.     //Set up the bounds rectangle for the source and mask gWorlds    
  140.     bndsRectSrc.top = 32;
  141.     bndsRectSrc.left = 64;
  142.     bndsRectSrc.bottom = 160;
  143.     bndsRectSrc.right = 192;
  144.     
  145.     bndsRectMask = bndsRectSrc;
  146.     
  147.     //Make the source,mask and destination rectangles the same 
  148.     // size as the associated gWorlds
  149.     srcRect = bndsRectSrc;
  150.     maskRect = bndsRectSrc;
  151.     destRect = bndsRectSrc;
  152.     
  153.     //Fetch the current port and gdevice and save them for later
  154.     GetGWorld(&currGWorldPort,&currGDevice);
  155.         
  156.     //Set up our source and mask gWorlds
  157.     gWorldErr = NewGWorld(&offscreenGWorldSource,pixelDepthSource,&bndsRectSrc,0,nil,flags);
  158.     if(gWorldErr != noErr)
  159.         DebugStr("\pthe first NewGWorld call failed");
  160.     else 
  161.     {
  162.         //lock the offscreen buffer
  163.         srcPMHdl = GetGWorldPixMap(offscreenGWorldSource);
  164.         pmLock = LockPixels(srcPMHdl);
  165.         if (!pmLock)
  166.             DebugStr("\pthe first LockPixels call failed");
  167.     }
  168.         
  169.     gWorldErr = NewGWorld(&offscreenGWorldMask,pixelDepthMask,&bndsRectMask,0,nil,flags);
  170.     if(gWorldErr != noErr)
  171.         DebugStr("\pthe second NewGWorld call failed"); 
  172.     else 
  173.     {
  174.         //lock the offscreen buffer
  175.         maskPMHdl = GetGWorldPixMap(offscreenGWorldMask);
  176.         pmLock = LockPixels(maskPMHdl);
  177.         if (!pmLock)
  178.             DebugStr("\pthe second LockPixels call failed");
  179.     }
  180.         
  181.     //Set the current graphics world to my offscreen source and draw into it
  182.     SetGWorld((CGrafPtr) offscreenGWorldSource,nil);
  183.     
  184.     srcPicHdl = GetPicture(srcPicID);
  185.     if (srcPicHdl==nil)
  186.         DebugStr("\pthe first call to GetPicture failed");
  187.     EraseRect(&srcRect);
  188.     HLock((Handle)srcPicHdl);
  189.     DrawPicture(srcPicHdl,&srcRect);
  190.     HUnlock((Handle) srcPicHdl);
  191.     
  192.     //Set the current graphics world to my offscreen mask and draw into it
  193.     SetGWorld((CGrafPtr)offscreenGWorldMask,nil);
  194.     GetPicture(maskPicID);
  195.     maskPicHdl = GetPicture(maskPicID);
  196.     if (maskPicHdl==nil)
  197.         DebugStr("\pthe second call to GetPicture failed");
  198.     EraseRect(&maskRect);
  199.     HLock((Handle)maskPicHdl);
  200.     DrawPicture(maskPicHdl,&maskRect);
  201.     HUnlock((Handle)maskPicHdl);
  202.  
  203.     //Set the current graphics port to my application window for drawing into
  204.     SetGWorld((CGrafPtr)mainWindow,nil);
  205.     BeginUpdate(mainWindow);
  206.  
  207.     //Now for the whole point of this. . .call CopyDeepMask
  208.     CopyDeepMask(
  209.                 (BitMap *) &(offscreenGWorldSource->portPixMap),
  210.                 (BitMap *) &(offscreenGWorldMask->portPixMap),
  211.                 &(mainWindow->portBits),
  212.                 &srcRect,
  213.                 &maskRect,
  214.                 &destRect,
  215.                 srcCopy,
  216.                 nil);
  217.                 
  218.     EndUpdate(mainWindow);
  219.     
  220.     //Unlock the offscreen buffer
  221.     UnlockPixels(srcPMHdl);
  222.     UnlockPixels(maskPMHdl);
  223.     
  224.     //Restore the saved port and gdevice
  225.     SetGWorld(currGWorldPort,currGDevice);
  226.         
  227.     //Dispose of the gWorlds
  228.     DisposeGWorld(offscreenGWorldSource);
  229.     DisposeGWorld(offscreenGWorldMask);
  230.  
  231. }